package org.ReportDesigner.reportModel;

import org.ReportDesigner.configurator.Configuration;
import org.ReportDesigner.controlModel.Type;
import com.itextpdf.text.*;
import com.itextpdf.tool.xml.ElementList;
import org.apache.log4j.Logger;

import java.util.List;

@Type(type = "repeated", subType = "repeatHeader", name = "repheader")
public class RepeatedHeader extends ReportElement {
    private static final Logger log = Logger.getLogger(RepeatedHeader.class);
    private String textRepeatedHeader;


    @Override
    public String getText() {
        return textRepeatedHeader;
    }

    @Override
    public void setText(String text) {
        this.textRepeatedHeader = text;
    }

    @Override
    public void addToDocument(Configuration configuration, Document document) throws DocumentException {
        List<Element> elementList = addTo(configuration);
        for (Element element: elementList){
            document.add(element);
        }
    }

    @Override
    public List<Element> addTo(Configuration configuration) {
        List<Element> elementList = new ElementList();
        //проверяем содержит клетка текст или нет
        if (!this.getText().isEmpty()){
            //добавляем текст в клетку
            Phrase phrase = new Phrase(this.getText(),
                    FontFactory.getFont(configuration.getFont(), configuration.getFontEncoding()));
            elementList.add(phrase);
        }
        //проверяем наличие дочерних элементов
        if (!this.getBody().isEmpty()){
            for (ReportElement element: this.getBody())
                elementList.addAll(element.addTo(configuration));
        }
        return elementList;
    }
}
